package com.singzi.javaSenior.reflect;

import org.junit.Test;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args){


    }
    @Test
    public void test01Cons() throws Exception{


    }
    @Test
    public void test02Clone() throws Exception{


    }
    @Test
    public void test03Serializable() throws Exception{
        Student student = new Student();
        student.setAge(18);
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("date.obj"));
        out.writeObject(student);
        out.close();
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("date.obj"));
        Student s = (Student)in.readObject();
        in.close();
        s.eat();
        System.out.println(s.getAge());
        Object obj = Class.forName("Student").newInstance();
        Method m = Class.forName("Student").getMethod("eat");
        m.invoke(obj);
    }
    @Test
    public void test04() throws Exception{
        Object obj = Class.forName("com.singzi.javaSenior.reflect.Student").newInstance();
        Method m = Class.forName("com.singzi.javaSenior.reflect.Student").getMethod("eat");
        m.invoke(obj);
        Student obj1 = (Student) Class.forName("com.singzi.javaSenior.reflect.Student").newInstance();
        obj1.eat();
    }
    @Test
    public void test05()throws Exception{
        Constructor<Student> constructor = Student.class.getConstructor();
        Student student = constructor.newInstance();
        student.eat();

    }
    @Test
    public void  test06GetClassName(){
        Student s1 = new Student();
        Class<? extends Student> c1 = s1.getClass();
        String name = c1.getName();
        System.out.println(name);
    }
    @Test
    public void test07GetFields() throws IllegalAccessException, InvocationTargetException {
        Student student = new Student("lucy", 20);
        student.setSex("女");
        Class c = student.getClass();
        Field[] fields = c.getFields();
        for (Field field : fields) {
            System.out.println(field.getName() + " :" + field.get(student));
        }
        Field[] fields1 = c.getDeclaredFields();
        for (Field field : fields1) {
            field.setAccessible(true);
            System.out.println(field.getName() + " :" + field.get(student));
        }
        Method[] methods = c.getDeclaredMethods();
        for (Method method : methods) {
            if ("say".equals(method.getName())) {
                method.setAccessible(true);
                System.out.println(method.invoke(student, "我喜欢你！"));
                break;
            }
        }
    }
}
class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    public String sex;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    private void say(String word){
        System.out.println(this.name + " said:" + " " + word);
    }
    public void eat(){
        System.out.println("学生吃！。。。。。。");
    }
}
